package com.fang.fen.blog.user.exception.handler;


import com.fang.fen.blog.common.exception.ApplicationException;
import com.fang.fen.blog.common.exception.SystemErrorException;
import com.fang.fen.blog.common.utils.ResponseUtils;
import com.fang.fen.blog.common.vo.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@ControllerAdvice
public class GlobalResponseAdvice implements ResponseBodyAdvice {

    private final static Logger logger = LoggerFactory.getLogger(GlobalResponseAdvice.class);

    @Autowired
    private Environment environment;

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        if (returnType.getMethodAnnotation(RequestMapping.class) != null) {

            String typeName = returnType.getGenericParameterType().getTypeName();

            //只处理非 Response 的返回值类型，包括void类型也会被处理
            if (!StringUtils.equals(typeName, Response.class.getTypeName())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Object beforeBodyWrite(Object returnValue, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {

        //构建结果对象
        return ResponseUtils.success(returnValue);
    }

    @ExceptionHandler(value = Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Response handler(Throwable e) {
        logger.error("[系统异常] {}", e);
        return new Response("-2", "未知错误");
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Response handler(Exception e) {
        logger.error("[系统异常] {}", e);
        return new Response("-1", "未知错误");
    }

    /**
     * 系统异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = SystemErrorException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Response handlerSystemErrorException(SystemErrorException e) {

        logger.error("[系统未知异常] {}", e);
        return new Response("-1", "系统错误");
    }

    /**
     * 系统业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = ApplicationException.class)
    @ResponseBody
    public Response handlerApplicationException(ApplicationException e) {
        return ResponseUtils.convertException(e);
    }


}
